# -*- coding: utf-8 -*-
import datetime
from telepathy._generated.errors import DoesNotExist
from django.http import Http404

from django.shortcuts import render_to_response
from dnu_schedule.models import Group
from models import Lesson
from copy import deepcopy

# Create your views here.

def show_schedule(request):

    days_count = 5
    lessons_count = 5

    schedule = range(days_count)

    for day_number in range(days_count):
        schedule[day_number] = []
        for lesson_number in range(lessons_count):
            schedule[day_number].append(Lesson.objects.filter(day=day_number, position=lesson_number).exclude(even=2))
            schedule[day_number].append(Lesson.objects.filter(day=day_number, position=lesson_number, even=2))



    group_names = []
    for lesson in schedule[0][0]:
        group_names.append(lesson.group.name)

    return render_to_response("global_schedule.html", {'schedule': schedule, 'group_names': group_names})

def show_schedule_by_group_name(request, group_name):
    group_name = group_name.upper()
    try :
        group = Group.objects.get(name = group_name)
    except Group.DoesNotExist:
        raise Http404()

    schedule = range(10)

    now = datetime.date.today()

    today = now.weekday()

    odd = (now.isocalendar()[1] % 2) == 1

    for lesson_number in range(5):
        schedule[lesson_number*2] = Lesson.objects.filter(position=lesson_number,group=group).exclude(even=2)
        schedule[lesson_number*2+1] = Lesson.objects.filter(position=lesson_number, even=2,group=group)

    return render_to_response("group_schedule.html", {'schedule' : schedule, 'group_name': group_name, 'odd' : odd, 'today' : today})
